<?php

/**
 * This is the model class for table "xman_billing".
 *
 * The followings are the available columns in table 'xman_billing':
 * @property integer $billing_id
 * @property string $billing_name
 * @property string $billing_date
 * @property string $billing_description
 * @property double $billing_cost
 * @property integer $billing_created_by_user_id
 * @property integer $billing_project_id
 *
 * The followings are the available model relations:
 * @property User $billingCreatedByUser
 * @property Project $billingProject
 * @property Expense[] $expenses
 * @property Invoice[] $invoices
 */
class Billing extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Billing the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_billing';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('billing_name, billing_date, billing_cost, billing_created_by_user_id, billing_project_id', 'required'),
			array('billing_created_by_user_id, billing_project_id', 'numerical', 'integerOnly'=>true),
			array('billing_cost', 'numerical'),
			array('billing_name', 'length', 'max'=>50),
			array('billing_description', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('billing_id, billing_name, billing_date, billing_description, billing_cost, billing_created_by_user_id, billing_project_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'billingCreatedByUser' => array(self::BELONGS_TO, 'User', 'billing_created_by_user_id'),
			'billingProject' => array(self::BELONGS_TO, 'Project', 'billing_project_id'),
			'expenses' => array(self::HAS_MANY, 'Expense', 'expense_billing_id'),
			'invoices' => array(self::HAS_MANY, 'Invoice', 'invoice_billing_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'billing_id' => 'Billing',
			'billing_name' => 'Billing Name',
			'billing_date' => 'Billing Date',
			'billing_description' => 'Billing Description',
			'billing_cost' => 'Billing Cost',
			'billing_created_by_user_id' => 'Billing Created By User',
			'billing_project_id' => 'Billing Project',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('billing_id',$this->billing_id);
		$criteria->compare('billing_name',$this->billing_name,true);
		$criteria->compare('billing_date',$this->billing_date,true);
		$criteria->compare('billing_description',$this->billing_description,true);
		$criteria->compare('billing_cost',$this->billing_cost);
		$criteria->compare('billing_created_by_user_id',$this->billing_created_by_user_id);
		$criteria->compare('billing_project_id',$this->billing_project_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}